{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit ExpressionManager;

interface

uses Expressions;

const
  MAX_STACK_DEPTH = 5; // Maximum recursive stack depth for expression evaluation

type
  TAHMExpressionManager = class
  private
    FStackDepth: Integer;
  protected
    function BeginEvaluation(Name: String): TAHMExpression;
  public
    function Evaluate(Name: String): String;
    function EvaluateCondition(Name: String): Boolean;
  end;

implementation

uses SysUtils, ApplicationManager, Logging;

function TAHMExpressionManager.BeginEvaluation(Name: String): TAHMExpression;
begin
  // Set default result - no expression found
  Result := nil;

  // Check for excessively recursive expression definitions
  if FStackDepth > MAX_STACK_DEPTH then
  begin
    LogWarn('Recursive expression definition: ' + Name);
    Exit;
  end;

  // Lookup requested expression
  Result := ApplicationManagerInstance.ThemeManager.InterfaceDescriptor.ExpressionList.GetExpression(Name);
  if not Assigned(Result) and LogDebugEnabled then
    LogDebug('Unknown expression requested for evaluation: ' + Name);
end;

function TAHMExpressionManager.Evaluate(Name: String): String;
var
  Expression: TAHMExpression;
begin
  Expression := BeginEvaluation(Name);
  if Assigned(Expression) then
    try
      // Increment stack depth whilst evaluating
      Inc(FStackDepth);
      Result := Expression.Evaluate;
    finally
      Dec(FStackDepth);
    end
  else
    Result := 'Unknown expression: ' + Name;
end;

function TAHMExpressionManager.EvaluateCondition(Name: String): Boolean;
var
  Expression: TAHMExpression;
begin
  Expression := BeginEvaluation(Name);
  if Assigned(Expression) then
    try
      // Increment stack depth whilst evaluating
      Inc(FStackDepth);
      Result := Expression.EvaluateBool;
    finally
      Dec(FStackDepth);
    end
  else
    Result := False;
end;


end.
